package cn.schoolwow.quickdao.module.initial.flow.database;

import cn.schoolwow.quickdao.domain.database.parent.DatabaseType;
import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;

public class SetCreateDatabaseStatementFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        DatabaseType databaseType = flowContext.checkInstanceData(DatabaseType.class);
        String databaseName = (String) flowContext.checkData("databaseName");

        String createDatabaseSQL = null;
        switch (databaseType){
            case H2:{createDatabaseSQL = "CREATE SCHEMA "+databaseName+";";}break;
            case MariaDB:
            case Mysql:{createDatabaseSQL = "CREATE DATABASE `"+databaseName+"` CHARACTER SET UTF8MB4 COLLATE UTF8MB4_UNICODE_CI;";}break;
            case Postgresql:
            case SQLServer:{createDatabaseSQL = "CREATE DATABASE \""+databaseName+"\";";}break;
            case Oracle:
            default:{
                flowContext.brokenCurrentCompositeBusiness("不支持的数据库类型!"+databaseType.name());
            }break;
        }
        flowContext.putTemporaryData("createDatabaseSQL", createDatabaseSQL);
    }

    @Override
    public String name() {
        return "设置创建数据库语句";
    }
}
